@echo off
chcp 65001 >nul
cls

echo ==================================================
echo         phpstudy MySQL远程连接问题修复工具(修复版)      
echo ==================================================

:: 检查是否以管理员身份运行
net session >nul 2>&1
if %errorLevel% NEQ 0 (
    echo 请以管理员身份运行此批处理文件！
    pause
    exit /b 1
)

:: MySQL配置 - 允许用户输入
set /p mysql_path=请输入MySQL安装路径(默认: D:\phpstudy_pro\Extensions\MySQL8.0.12): 
if "%mysql_path%"=="" set mysql_path=D:\phpstudy_pro\Extensions\MySQL8.0.12
set config_file=%mysql_path%\my.ini

:: 检查路径是否存在
if not exist "%mysql_path%" (
    echo 错误: MySQL路径不存在，请检查后重试。
    pause
    exit /b 1
)

if not exist "%config_file%" (
    echo 错误: 配置文件不存在，请检查路径是否正确。
    pause
    exit /b 1
)

:: 1. 备份配置文件
echo [1/4] 备份配置文件...
copy "%config_file%" "%config_file%.bak.phpstudy" >nul 2>&1
if %errorLevel% NEQ 0 (
    echo ✗ 配置文件备份失败
) else (
    echo ✓ 配置文件已备份
)

:: 2. 修改MySQL配置文件(bind-address)
echo [2/4] 修改MySQL配置...

:: 检查是否已存在bind-address
findstr /i "bind-address" "%config_file%" >nul
if %errorLevel% EQU 0 (
    :: 存在则修改
    (for /f "tokens=*" %%a in ('type "%config_file%"') do (
        echo %%a | findstr /c:"bind-address" >nul
        if errorlevel 1 (
            echo %%a
        ) else (
            echo bind-address = 0.0.0.0
        )
    )) > "%config_file%.tmp"
) else (
    :: 不存在则添加
    echo. >> "%config_file%"
    echo # 允许远程连接 >> "%config_file%"
    echo bind-address = 0.0.0.0 >> "%config_file%"
    type "%config_file%" > "%config_file%.tmp"
)
move /y "%config_file%.tmp" "%config_file%" >nul 2>&1
if %errorLevel% NEQ 0 (
    echo ✗ MySQL配置更新失败
) else (
    echo ✓ MySQL配置已更新，bind-address已设置为0.0.0.0
)

:: 3. 配置Windows防火墙
echo [3/4] 检查防火墙规则...
netsh advfirewall firewall show rule name="MySQL" >nul 2>&1
if %errorLevel% EQU 0 (
    echo ✓ 防火墙规则已存在
) else (
    echo ✗ 防火墙规则不存在，正在添加...
    netsh advfirewall firewall add rule name="MySQL" dir=in action=allow protocol=TCP localport=3306 >nul 2>&1
    if %errorLevel% NEQ 0 (
        echo ✗ 入站防火墙规则添加失败
    )
    netsh advfirewall firewall add rule name="MySQL" dir=out action=allow protocol=TCP localport=3306 >nul 2>&1
    if %errorLevel% NEQ 0 (
        echo ✗ 出站防火墙规则添加失败
    ) else (
        echo ✓ 防火墙规则已添加
    )
)

:: 4. 重启MySQL服务
echo [4/4] 重启MySQL服务...
set /p mysql_service=请输入MySQL服务名称(默认: MySQL8.0): 
if "%mysql_service%"=="" set mysql_service=MySQL8.0

net stop "%mysql_service%" >nul 2>&1
if %errorLevel% NEQ 0 (
    echo ✗ MySQL服务停止失败，请手动停止服务
) else (
    echo ✓ MySQL服务已停止
    net start "%mysql_service%" >nul 2>&1
    if %errorLevel% NEQ 0 (
        echo ✗ MySQL服务启动失败，请手动启动服务
    ) else (
        echo ✓ MySQL服务已重启
    )
)

:: 5. 创建远程用户并授权
set /p root_password=请输入MySQL root用户密码: 

echo [5/5] 配置远程用户权限...
"%mysql_path%\bin\mysql.exe" -u root -p%root_password% -e "CREATE USER IF NOT EXISTS 'remote_user'@'%%' IDENTIFIED BY 'remote_password123';" >nul 2>&1
if %errorLevel% NEQ 0 (
    echo ✗ 创建远程用户失败
) else (
    "%mysql_path%\bin\mysql.exe" -u root -p%root_password% -e "GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%%' WITH GRANT OPTION;" >nul 2>&1
    if %errorLevel% NEQ 0 (
        echo ✗ 授权远程用户失败
    ) else (
        "%mysql_path%\bin\mysql.exe" -u root -p%root_password% -e "FLUSH PRIVILEGES;" >nul 2>&1
        echo ✓ 远程用户已创建并授权
    )
)

:: 显示本机IP地址
echo.
echo 获取本机IP地址...
ipconfig | findstr /i "IPv4"

if %errorLevel% NEQ 0 (
    echo ✗ 获取IP地址失败
)

echo.
echo ==================================================
echo                   修复完成                         
echo ==================================================
echo 连接信息：
echo 主机: 上面显示的IPv4地址
echo 端口: 3306
echo 用户名: remote_user
echo 密码: remote_password123
echo ==================================================

pause